package com.briup.test;

/**
 * @author 甜圆
 * @version v1.0.1
 * @date 2025/8/18 17:10
 * @package com.briup.a01mymap
 * @copyright 甜圆
 */
// Student类实现
public class Student {
    // 成员属性
    private String name;
    private int age;

    // 构造方法
    public Student(String name, int age) throws AgeOutRangeException {
        this.name = name;
        // 验证年龄范围
        if (age < 16 || age > 79) {
            throw new AgeOutRangeException(age);
        }
        this.age = age;
    }

    // Getter和Setter方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    // 设置年龄时也需要验证范围
    public void setAge(int age) throws AgeOutRangeException {
        if (age < 16 || age > 79) {
            throw new AgeOutRangeException(age);
        }
        this.age = age;
    }

    // 重写toString方法，便于打印信息
    @Override
    public String toString() {
        return "Student{name='" + name + "', age=" + age + "}";
    }

    // 测试示例
    public static void main(String[] args) {
        try {
            // 正常情况
            Student s1 = new Student("张三", 22);
            System.out.println(s1);

            // 测试setAge方法
            s1.setAge(23);
            System.out.println("修改年龄后: " + s1);

            // 异常情况：年龄太小
            Student s2 = new Student("李四", 15);
        } catch (AgeOutRangeException e) {
            System.out.println("错误: " + e.getMessage());
        }

        try {
            // 异常情况：年龄太大
            Student s3 = new Student("王五", 80);
        } catch (AgeOutRangeException e) {
            System.out.println("错误: " + e.getMessage());
        }
    }
}
